package com.fzq.service.impl;

import com.fzq.config.MaskingStrategyFactory;
import com.fzq.config.Sensitive;
import com.fzq.dao.mapper.SensitiveUserMapper;
import com.fzq.dao.po.SensitiveUser;
import com.fzq.service.MaskingStrategy;
import com.fzq.service.SensitiveUserService;
import com.fzq.utils.MaskUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;

@Slf4j
@Service
public class SensitiveUserServiceImpl implements SensitiveUserService {
    @Autowired
    SensitiveUserMapper SensitiveUserMapper;
    @Autowired
    MaskUtil maskUtil;


    /**
     * 查询所有用户信息,并且脱敏
     * @return
     */
    @Override
    public List<SensitiveUser> getAllList() {
        List<SensitiveUser> allList = SensitiveUserMapper.getAllList();
        if (allList != null && !allList.isEmpty()) {
            for (SensitiveUser user : allList) {
                // 获取所有字段（包括私有字段）
                Field[] fields = user.getClass().getDeclaredFields();
                for (Field field : fields) {
                    // 检查字段是否包含 @Sensitive 注解
                    if (field.isAnnotationPresent(Sensitive.class)) {
                        Sensitive annotation = field.getAnnotation(Sensitive.class);
                        Sensitive.SensitiveType type = annotation.type();
                        // 根据敏感类型获取脱敏策略
                        MaskingStrategy strategy = MaskingStrategyFactory.getStrategy(type);
                        field.setAccessible(true); // 允许访问私有字段
                        try {
                            Object originalValue = field.get(user);
                            if (originalValue instanceof String) {
                                // 应用脱敏策略（假设策略需要原始值和可选参数，这里传null）
                                String maskedValue = strategy.mask((String) originalValue, null);
                                field.set(user, maskedValue);
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace(); // 处理异常，例如日志记录
                            log.error("脱敏异常：" + e.getMessage());
                        }
                    }
                }
            }
        }
        return allList;
    }

    /**
     * 查询所有用户信息
     * @return
     */
    @Override
    public List<SensitiveUser> getList() {
        return SensitiveUserMapper.getAllList();
    }

    /**
     * 查询所有用户信息
     * @return
     */
    @Override
    public List<SensitiveUser> getList2() {
        List<SensitiveUser> allList = SensitiveUserMapper.getAllList();
        if (allList != null && !allList.isEmpty()) {
            for (SensitiveUser user : allList) {
                try {
                    maskUtil.maskObject(user);
                }catch (Exception e){
                    e.printStackTrace();
                    log.error("脱敏异常：" + e.getMessage());
                }
            }
        }
        return allList;
    }
}
